package utils;

import java.util.Arrays;
import java.util.List;
import java.util.function.Predicate;


/**
 * Java 8中使用lambda表达式的Map和Reduce示例
 */
public class App5 {

    public static void main(String[] args) {
        // 不使用lambda表达式为每个订单加上12%的税
        List<Integer> costBeforeTax = Arrays.asList(100, 200, 300, 400, 500);
        for (Integer cost : costBeforeTax) {
            double price = cost + .12*cost;
            System.out.println(price);
        }
        System.out.println("--------------");
        //使用lambda表达式
        costBeforeTax.stream().map(cost -> cost + .12*cost).forEach(System.out::println);

        // 为每个订单加上12%的税
        // 老方法：
        List<Integer> costBeforeTax1 = Arrays.asList(100, 200, 300, 400, 500);
        double total = 0;
        for (Integer cost : costBeforeTax1) {
            double price = cost + .12*cost;
            total = total + price;
        }
        System.out.println("Total : " + total);

        //lambda
        Double total1 = costBeforeTax1.stream().map(a -> a + .12*a).reduce((a,b) -> a+b).get();
        System.out.println("Total1 :"+total1);
    }


}
